package cn.lwalan.commonUtils;

import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.jdom.input.SAXBuilder;
import com.alibaba.fastjson.JSONObject;

public class XmlUtil {
	public static Map<String,String> xml2Map(String xml) {  
		try {
			Map<String,String> list = new HashMap<String,String>();
			Document document = DocumentHelper.parseText(xml);
			Element nodesElement = document.getRootElement();
			List nodes = nodesElement.elements();
			getMap(list, nodes);
			return list;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

	private static void getMap(Map<String, String> map, List nodes) {
		for (Iterator its = nodes.iterator(); its.hasNext();) {
			Element nodeElement = (Element) its.next();
			if (nodeElement.elements().isEmpty()) {
				map.put(nodeElement.getName(), nodeElement.getText());
			} else {
				getMap(map, nodeElement.elements());
			}
		}
	}

	public static String xmlToJsonStr(String xml) {
		JSONObject obj = new JSONObject();
		try {
			InputStream is = new ByteArrayInputStream(xml.getBytes("utf-8"));
			SAXBuilder sb = new SAXBuilder();
			org.jdom.Document doc = sb.build(is);
			org.jdom.Element root = doc.getRootElement();
			obj.put(root.getName(), iterateElement(root));
			Map map = iterateElement(root);
			return obj.toString();
		} catch (Exception e) {
			return null;
		}
	}

	public static Map xmlToMap(String xml) {
		try {
			InputStream is = new ByteArrayInputStream(xml.getBytes("utf-8"));
			SAXBuilder sb = new SAXBuilder();
			org.jdom.Document doc = sb.build(is);
			org.jdom.Element root = doc.getRootElement();
			Map map = iterateElement(root);
			return map;
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}

	private static Map iterateElement(org.jdom.Element element) {
		List jiedian = element.getChildren();
		org.jdom.Element et = null;
		Map obj = new HashMap();
		List list = null;
		for (int i = 0; i < jiedian.size(); i++) {
			list = new LinkedList();
			et = (org.jdom.Element) jiedian.get(i);
			if (et.getTextTrim().equals("")) {
				if (et.getChildren().size() == 0)
					continue;
				if (obj.containsKey(et.getName())) {
					list = (List) obj.get(et.getName());
				}
				if(!"array".equals(et.getAttributeValue("class"))){
					obj.put(et.getName(), iterateElement(et));
				}else{
					list.add(iterateElement(et));
					obj.put(et.getName(), list);
				}
			} else {
//				if (obj.containsKey(et.getName())) {
//					list = (List) obj.get(et.getName());
//				}
//				list.add(et.getTextTrim());
//				System.out.println("list======="+et.getTextTrim());
				obj.put(et.getName(), et.getTextTrim());
			}
		}
		return obj;
	}
	
	public static void main(String[] args) {
		String xml = "<EPOSPROTOCOL><RSPCOD>000000</RSPCOD><RSPMSG>查询我的账单信息成功！</RSPMSG><TRANDETAILS><TRANDETAIL class=\"array\"><MERNAM>彭亮</MERNAM><OPERSTS>1</OPERSTS><OPERBTYP>0</OPERBTYP></TRANDETAIL><TRANDETAIL class=\"array\"><MERNAM>彭亮</MERNAM><OPERSTS>1</OPERSTS><OPERBTYP>2</OPERBTYP><TARGATCNAM>彭亮</TARGATCNAM></TRANDETAIL></TRANDETAILS></EPOSPROTOCOL>";
		Map map = xmlToMap(xml);
		System.out.println(map);
	}

}
